package org.douxiaoban.client.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.mobclick.android.MobclickAgent;
import org.douxiaoban.client.R;
import org.douxiaoban.client.api.MailData;
import org.douxiaoban.client.app.Assistant;
import org.douxiaoban.client.util.ThreadTool;
import org.douxiaoban.client.widget.PopupRecommend;
import org.douxiaoban.client.widget.TitleStyle;


public class OneMailInfoActivity extends BaseActivity{
	private Handler mHandler;
	private boolean mIsInbox;
	private PopupRecommend mPopup;
	private String mId;
	private TitleStyle mTitle;
	private View mProgress;
	private View mOneMail;
	
	private ProgressDialog mPd;
	

	@Override
	public void onCreate(Bundle b){
		super.onCreate(b);
		Bundle bundle = getIntent().getExtras();
		mId = bundle.getString(Assistant.KEY_MAIL_ID);
		mIsInbox = bundle.getBoolean(Assistant.KEY_IS_INBOX);
		
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.one_mail_info);
	    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
        mTitle = new TitleStyle(this);
        mTitle.initTitleStyle();
        mTitle.initPageController(1,1);
        mTitle.setTitle(getString(R.string.titleOneMail));
		initHandler();
		mPopup = new PopupRecommend(this);
		mProgress = findViewById(R.id.oneMailprogressBar);
		mOneMail = findViewById(R.id.oneMail);
		ThreadTool.instace(this).getOneMail(mHandler, mId);
	}
	
	private void initHandler(){
		mHandler = new Handler(){
			@Override
			public void handleMessage(Message msg){
				Bundle bundle = msg.getData();
				switch(msg.what){
				case Assistant.TOOLS_MSG_ONE_MAIL:
					ThreadTool.MailSerialz serialz =
								(ThreadTool.MailSerialz)bundle.getSerializable(Assistant.TOOLS_KEY_ONE_MAIL);
					
					updateMailInfo(serialz.mMailData);
					break;
				case Assistant.TOOLS_MSG_IMAGE:
					ThreadTool.ImageSerialz image =
								(ThreadTool.ImageSerialz)bundle.getSerializable(Assistant.TOOLS_KEY_IMAGE);
					Bitmap avatar = image.m_bitmap;
					updateAvatar(avatar);
					break;
				case Assistant.MSG_DEL_MAIL:
					mPd.dismiss();
					if(bundle.getBoolean(Assistant.KEY_SUCCESS)){
						//提示删除成功					
						setResult(Assistant.DEL_MAIL_SUCCESS);
						finish();	
					}else{
						//删除不成功
						setResult(Assistant.DEL_MAIL_UN_SUCCESS);
						finish();
					}
				}
			}
		};
	}
	
	private void updateMailInfo(MailData mailData){
		if(null == mailData){
			//提示木有取到数据
			return;
		}
		mProgress.setVisibility(View.GONE);
		mOneMail.setVisibility(View.VISIBLE);
		TextView sendOrRec = (TextView)findViewById(R.id.mailToOrFrom);
		TextView name = (TextView)findViewById(R.id.oneMailSender);
		final String strName;
		final String strId;
		if(mIsInbox){
			sendOrRec.setText("来自: ");
			strName = mailData.mSenderName;
			strId = mailData.mSenderId;
			ThreadTool.instace(this).getImage(mailData.mSenderAvatarLink, mHandler, -1);
		}else{
			sendOrRec.setText("发往: ");
			strName = mailData.mRecName;
			strId = mailData.mRecId;
			ThreadTool.instace(this).getImage(mailData.mRecAvatarLink, mHandler, -1);
		}
		name.setText(strName);
		TextView publicTime = (TextView)findViewById(R.id.oneMailPublicTime);
		publicTime.setText(mailData.mPublicTime);
		
		TextView title = (TextView)findViewById(R.id.oneMailTitle);
		title.setText(mailData.mTitle);
		
		TextView content = (TextView)findViewById(R.id.oneMailContetn);
		content.setText(mailData.mContent);	
		
		final Button repBtn = (Button)findViewById(R.id.oneMailBtnRec);
		repBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View view){
//				LayoutInflater flate = LayoutInflater.from(OneMailInfoActivity.class);
//				View v = flate.inflate(R.layout.one_mail_info, null);
				//mPopup.showSendMail(repBtn, strId, strName);
				Intent intent = new Intent();
				intent.putExtra(Assistant.KEY_USER_ID, strId);
				intent.putExtra(Assistant.KEY_USER_NAME, strName);
				intent.setClass(OneMailInfoActivity.this, SendMailActivity.class);
				startActivityForResult(intent,Assistant.REQ_REP_CODE);
				
			}

		});
		Button delBtn = (Button)findViewById(R.id.oneMailBtnDel);
		delBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				ThreadTool tool = ThreadTool.instace(OneMailInfoActivity.this);
				//传给本类的handler，最后一个参数无效！
				//最后一个参数，MailActivity中的mCurrentIndex替代
				mPd = ProgressDialog.show(OneMailInfoActivity.this, "", "正在删除...");
				tool.delMail(mHandler, mId, -1);
			}
		});
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data){
		super.onActivityResult(requestCode, resultCode, data);
		if(Assistant.REQ_REP_CODE == requestCode && Assistant.REP_SUCCESS == resultCode){
			mPopup.showToastWindow(Assistant.SUCCESS_POP, "豆邮发送成功");
		}else if(Assistant.REQ_REP_CODE == requestCode && Assistant.REP_UN_SUCCESS == resultCode){
			mPopup.showToastWindow(Assistant.FAIL_POP, "豆邮发送失败");
		}
	}
	
	private void updateAvatar(Bitmap avatar){
		ImageView imageView = (ImageView)findViewById(R.id.oneMailAvatar);
		imageView.setImageBitmap(avatar);
	}

      @Override
    protected void onResume() {
        MobclickAgent.onResume(this);
        super.onResume();    //Super Method super.onResume();
    }

    @Override
    protected void onPause() {
        MobclickAgent.onPause(this);
        super.onPause();    //Super Method super.onPause();
    }
}